# 1、使用程序进行发起请求
# 2、带服务器发送过来的响应进行解析
# 3、存储数据
import requests
import re
import csv
url="https://movie.douban.com/top250"
headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}
resp=requests.get(url,headers=headers)
# 打印响应的结果
# print(resp.text)
obj=re.compile(r'<div class="item">.*?<span class="title">(?P<moviename>.*?)</span>.*?<p class="">'
               r'.*?<br>(?P<year>.*?)&nbsp;.*?<span class="rating_num" property="v:average">(?P<ratingnum>.*?)</span>'
               r'.*?<span>(?P<score>.*?)人评价</span>',re.S)
movieMsg=obj.finditer(resp.text)
# 将过滤后的数据保存在csv文件中
f=open("doubanTop250.csv",mode="w")
csvWriter=csv.writer(f)
for i in movieMsg:
    # print(i.group("moviename"))
    # print(i.group("year").strip())
    # print(i.group("ratingnum"))
    # print(i.group("score"))
    # 将上面筛选过后的内容进行转换为字典格式
    dict1=i.groupdict()
    # 将年份数据前的换行符去掉
    dict1["year"]=dict1["year"].strip()
    csvWriter.writerow(dict1.values())
    pass
f.close()
resp.close()